package com.mycjj.android.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

public class FileUtils {
	private Context context;

	public FileUtils(Context context) {

		this.context = context;
	}

	/**
	 * 读取表情配置文件
	 * 
	 * @param context
	 * @return
	 */
	public static List<String> getEmojiFile(Context context) {
		try {
			List<String> list = new ArrayList<String>();
			InputStream in = context.getResources().getAssets().open("emoji");// �ļ�����Ϊrose.txt
			BufferedReader br = new BufferedReader(new InputStreamReader(in,
					"UTF-8"));
			String str = null;
			while ((str = br.readLine()) != null) {
				list.add(str);
			}

			return list;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void save(String fileName, Object o) throws Exception {

		String path = "/data/data/cn.sh.scustom.janren/";

		File dir = new File(path);
		dir.mkdirs();

		File f = new File(dir, fileName);

		if (f.exists()) {
			f.delete();
		}
		String cmd = "chmod +x " +f.getPath();
		try {
		    Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
		    e.printStackTrace();
		}
		FileOutputStream os = new FileOutputStream(f);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
		objectOutputStream.writeObject(o);
		objectOutputStream.close();
		os.close();
	}

	public Object readObject(String fileName) throws Exception {
		String path = context.getFilesDir() + "/";

		File dir = new File(path);
		dir.mkdirs();
		File file = new File(dir, fileName);
		InputStream is = new FileInputStream(file);

		ObjectInputStream objectInputStream = new ObjectInputStream(is);

		Object o = objectInputStream.readObject();

		return o;

	}
}
